package com.bjpowernode.filter;


import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;

public class OneFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 拦下当前请求对象，filterChain放行
        String age = servletRequest.getParameter("age");
        if (Integer.valueOf(age) < 70){
            // 将请求对象响应对象，交还给Tomcat 继续调用资源文件
            filterChain.doFilter(servletRequest,servletResponse);//放行
        }else {
            // 过滤器代替http服务器拒绝被刺请求
            // 响应体有数据，就返回了。
            servletResponse.setContentType("text/html;charset=utf-8");
            PrintWriter out = servletResponse.getWriter();
            out.print("<center><font style = 'color:red;font-size:40px'>大爷，珍爱生命啊！</font></center>");
        }
    }

    @Override
    public void destroy() {

    }
}
